package com.goblin.model.uaa.repository;

import com.goblin.common.response.PageHelper;
import com.goblin.common.response.Paged;
import com.goblin.model.base.BaseRepositoryImpl;
import com.goblin.model.uaa.po.QUser;
import com.goblin.model.uaa.po.User;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.JPQLQuery;

import javax.persistence.EntityManager;

public class UserRepositoryImpl extends BaseRepositoryImpl<User, Integer>
        implements UserRepositoryCustom {

    public UserRepositoryImpl(EntityManager entityManager) {
        super(User.class, entityManager);
    }

    @Override
    public Paged<User> paged(PageHelper pageable) {
        JPQLQuery<User> query = this.jpaQueryFactory.selectFrom(QUser.user)
                .where(QUser.user.deleteTime.isNull()).orderBy(QUser.user.id.desc());
        query = (JPQLQuery<User>) getQuerydsl(pageable, query);
        QueryResults<User> results = query.fetchResults();
        results.getResults().forEach((e) -> {
            e.setPassword(null);
        });
        return new Paged<User>(results.getTotal(), results.getResults());
    }
}
